package com.itheima.edu.info.manager.dao;

import com.itheima.edu.info.manager.domain.Teacher;

public class TeacherDao {
    private static Teacher[] teachers = new Teacher[5];

    public Teacher[] findAllTeacher() {
        return teachers;
    }

    public boolean addTeacher(Teacher teacher) {
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            if (teachers[i] == null) {
                index = i;
                teachers[i] = teacher;
                break;
            }
        }

        return index != -1;
    }

    public void deleteTeacherById(String delId) {
        int index = getIndex(delId);
        teachers[index] = null;
    }

    private int getIndex(String id) {
        int index = -1;
        for (int i = 0; i < teachers.length; i++) {
            Teacher teacher = teachers[i];
            if (teacher != null && teacher.getId().equals(id)) {
                index = i;
                break;
            }
        }
        return index;
    }

    public void updateTeacher(Teacher teacher) {
        int index = getIndex(teacher.getId());
        teachers[index] = teacher;
    }
}
